<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>deepFreeze</title>
</head>

<body>
    <div>被冻结的对象: <span id="text">[1, [2, 3]]</span></div>
    <button type="button" id="test">点击我修改对象</button>
    <script>
        const deepFreeze = obj => {
            Object.keys(obj).forEach(prop => {
                if (typeof obj[prop] === 'object') {
                    deepFreeze(obj[prop]);
                }
            });
            return Object.freeze(obj);
        };
        const val = deepFreeze([1, [2, 3]]);
        const btn = document.getElementById('test');
        const text = document.getElementById('text');
        btn.onclick = () => {
            val[0] = 1;
            text.textContent = '对象修改失败:' + JSON.stringify(val);
        }
    </script>
</body>

</html>